package com.qf.cloud2204.microb.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    EurekaClient eurekaClient;

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/test1")
    public String test1(){

        // 调用 微服务A 的接口 ：get 请求  /a/hello

        // 根据服务的名字，获取服务的 ip地址和端口号
        InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("MICROA", false);

        String homePageUrl = instanceInfo.getHomePageUrl();
        String hostName = instanceInfo.getHostName();
        String ipAddr = instanceInfo.getIPAddr();
        int port = instanceInfo.getPort();


        // 组织请求的 url
        String url = homePageUrl+"a/hello";

        // 发送 http请求并接收响应数据
        String result = restTemplate.getForObject(url, String.class);
        System.out.println(result);

        return result;
    }

}
